package admin

import (
	"fmt"
	"github.com/xuri/excelize/v2"
	"new_club_recruitment/model/admin"
)

type ExcelService struct{}

func (exa *ExcelService) ParseInfoList2Excel(infoList []admin.SysAdmin, filePath string) (err error) {
	excel := excelize.NewFile()
	err = excel.SetSheetRow("Sheet1", "A1", &[]string{"ID", "账号名", "邮箱", "手机号", "权限"})
	if err != nil {
		return
	}
	for i, menu := range infoList {
		axis := fmt.Sprintf("A%d", i+1)
		err = excel.SetSheetRow("Sheet1", axis, &[]interface{}{
			menu.ID,
			menu.AdminName,
			menu.Email,
			menu.Phone,
			menu.Permission,
		})
		if err != nil {
			return err
		}
	}
	err = excel.SaveAs(filePath)
	return err
}

/*func (exa *ExcelService) ParseExcel2InfoList() ([]admin.SysAdmin, error) {
	skipHeader := true
	fixedHeader := []string{"ID", "账号名", "邮箱", "手机号", "密码", "权限", "token令牌"}
	file, err := excelize.OpenFile(global.GVA_CONFIG.Excel.Dir + "ExcelImport.xlsx")
	if err != nil {
		return nil, err
	}
	menus := make([]admin.SysAdmin, 0)
	rows, err := file.Rows("Sheet1")
	if err != nil {
		return nil, err
	}
	for rows.Next() {
		row, err := rows.Columns()
		if err != nil {
			return nil, err
		}
		if skipHeader {
			if exa.compareStrSlice(row, fixedHeader) {
				skipHeader = false
				continue
			} else {
				return nil, errors.New("Excel格式错误")
			}
		}
		if len(row) != len(fixedHeader) {
			continue
		}
		id, _ := strconv.Atoi(row[0])
		menu := admin.SysAdmin{
			GVA_MODEL: global.GVA_MODEL{
				ID: uint(id),
			},
			AdminName:  row[1],
			Email:      row[2],
			Phone:      row[3],
			Password:   row[4],
			Permission: row[5],
			Token:      row[6],
		}
		menus = append(menus, menu)
	}
	return menus, nil
}

func (exa *ExcelService) compareStrSlice(a, b []string) bool {
	if len(a) != len(b) {
		return false
	}
	if (b == nil) != (a == nil) {
		return false
	}
	for key, value := range a {
		if value != b[key] {
			return false
		}
	}
	return true
}*/
